package com.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

import org.apache.log4j.Logger;



public class JspMaker {
	
	public static Logger logger = Logger.getLogger("JspMaker.java");
	
	final private String header = "<%@ page language='java' contentType='text/html; charset=UTF-8' pageEncoding='UTF-8'%>\r\n<%\r\n";
	final private String footer = "};\r\n%>\r\n<%@include file='../main/top.jsp'%>";
	private BufferedWriter bw = null;

	//jsp-file 생성
	public void makeJspFile(String content, String userId, String root){
		
		try {
			bw = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(root + "/WEB-INF/sitemesh/menu/"+ userId+"_menu.jsp",false),"UTF8"));
				bw.write(header);
				bw.write(content);
				bw.write(footer);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if( bw != null){
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}// end if
		}//end try
	}
	
	//jsp-file 삭제
	public void deleteJspFile(String root, String userId){
		root = root + "/WEB-INF/sitemesh/menu/"+ userId+"_menu.jsp";
		File file = new File(root);
		if(file.exists())
		{
			if(file.delete())
			{
				logger.info("첨부파일 삭제");
			}else
			{
				logger.info("첨부파일 삭제 에러");
			};
		}// end if
	}
}
